/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package main.environment;

import java.awt.Image;
import main.common.Tile;
import main.entities.EntityHandler;
import main.entities.actors.PlayerActor;

/**
 *
 * @author Fox
 */
public class DefaultEnvironmentImpl implements IEnvironment {
    
    private Tile[][] floor;
    private Tile[][] objects;
    private EntityHandler entities;
            
    private final int width = 32;
    private final int height = 32;
    private final int dimension = 16;

    public DefaultEnvironmentImpl() {
        this.objects = new Tile[width][height];
        this.floor = new Tile[width][height];
        entities = new EntityHandler();
        entities.addEntity(new PlayerActor());
    }
    
    @Override
    public EntityHandler getEntityHandler() {
        return entities;
    }

    @Override
    public boolean collisionAt(Location l) {
        //lol, fuck you arrays
        try {
            Tile t = objects[l.getX()][l.getY()];
            
            if (t != null) {
                return true;
            } else {
                return false;
            }
        } catch (java.lang.ArrayIndexOutOfBoundsException ex) {
            return false;
        }
    }

    @Override
    public Location moveTo(Location from, Location to) {
        if (collisionAt(to)) {
            return from;
        } else {
            try {
                objects[from.getX()][from.getY()] = null;
                objects[to.getX()][to.getY()] = to.getOwner();

            } catch (java.lang.ArrayIndexOutOfBoundsException ex) {
                return to;
            }
            return to;
        }
    }

    @Override
    public Image blockAt(Location l) {
        Tile s = objects[l.getX()][l.getY()];
        
        if (s == null) {
            return null;
        } else {
            return objects[l.getX()][l.getY()].getImg();
        }
    }
    
    @Override
    public Image floorAt(Location l) {
        Tile s = floor[l.getX()][l.getY()];
        
        if (s == null) {
            return null;
        } else {
            return floor[l.getX()][l.getY()].getImg();
        }
    }

    @Override
    public int getWidth() {
        return width;
    }

    @Override
    public int getHeight() {
        return height;
    }

    @Override
    public int getDimension() {
        return dimension;
    }
    
    @Override
    public void updateBlock(Location l) {
        objects[l.getX()][l.getY()] = l.getOwner();
    }

    @Override
    public void updateFloor(Location l) {
        floor[l.getX()][l.getY()] = l.getOwner();
    }

    @Override
    public void run() {
        entities.run(this);
    }
}
